<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>新建回款计划</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_kuan','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_kuan','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="shiid" class="layui-input shiid" onclick="choose_contract_shi_and_bu()">
                            <div class="choose_div" id="shiid" key="1"></div>
                            <span class="choose_one" onclick="choose_contract_shi_and_bu()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="shi_money" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="shi_number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">发包方(甲方)</label>
                        <div class="layui-input-block">
                            <input type="text" id="shi_faer" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>回款计划</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>回款比例</th>
                            <th><text>*</text>回款金额(元)</th>
                            <th><text>*</text>计划回款时间</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="kuan_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


    </div>
</form>
<script>

    function choose_contract_shi_and_bu(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_contract_shi.php?dom=shiid&page=contract_kuan&enginid='+enginid,'80%','80%')
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_contract_kuan_add}).done(function(res){
            if (res.code === 2000) {

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="number" class="layui-input rate" oninput="auto_jsuan_hui_money(this)"></td>';
                listAddStr += '<td><input type="number" placeholder="自动计算,可修改" class="layui-input money"></td>';
                listAddStr += '<td><input type="text" class="layui-input dt bdate_ymd" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);


                public_upload_file_load('upload_files');
                render_date();
                render_from();
                initParseMoney();
            }
        });

        form.on('submit(add)', function(data){
            parseMoney(data);
            var validRes = $("#commentForm").valid();

            data.field.enginid = get_choose_val('enginid');
            formValid('enginid');
            if(data.field.enginid == undefined || !data.field.enginid){
                validRes = false;
            }

            data.field.shiid = get_choose_val('shiid');
            formValid('shiid');
            if(data.field.shiid == undefined || !data.field.shiid){
                validRes = false;
            }
            if(!validRes){return false;}


            var list = [''];
            try {
                $('#kuan_list tr').each(function (i) {
                    var rate = $(this).find('.rate').val();
                    var money = $(this).find('.money').val();
                    var dt = $(this).find('.dt').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!rate || rate < 0 || rate > 100){
                        throw new Error('第'+index+'行，请输入回款比例(0-100)');
                    }
                    if(!money || money == 0 || money == 0.00){
                        throw new Error('第'+index+'行，请输入回款金额');
                    }
                    if(!dt){
                        throw new Error('第'+index+'行，请选择计划回款时间');
                    }

                    list.push({
                        rate:rate,
                        money:money,
                        dt:dt,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            facade.ajax({url: ask_contract_kuan_add, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });



    function auto_jsuan_hui_money(Obj){
        var shi_money = parseValidNum($("#shi_money").val());
        var rate = parseValidNum($(Obj).val());

        var money = parseFloat(shi_money * rate / 100).toFixed(2);
        $(Obj).parents('tr').find('.money').val(money);
    }





    function choose_enginid_delete() {
        $("#shiid").html('');
        $("#shi_money").val('');
        $("#shi_number").val('');
        $("#shi_faer").val('');
        $("#kuan_list").html('');
    }


    function choose_shiid_delete() {
        $("#shi_money").val('');
        $("#shi_number").val('');
        $("#shi_faer").val('');
        $("#kuan_list").html('');
    }






    var ctrlv_field_name = [
        {field: 'rate', name:'回款比例',type:'number'},
        {field: 'money', name:'回款金额(元)',type:'number'},
        {field: 'dt', name:'计划回款时间',type:'date'},
        {field: 'explain', name:'备注'},
    ];

    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#kuan_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="number" class="layui-input rate" value="'+list[key].rate+'" oninput="auto_jsuan_hui_money(this)"></td>';
                str += '<td><input type="number" placeholder="自动计算,可修改" value="'+list[key].money+'" class="layui-input money"></td>';
                str += '<td><input type="text" class="layui-input dt bdate_ymd" value="'+list[key].dt+'" readonly></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#kuan_list").append(str);
            }
        }

        initParseMoney();
        render_date();
        render_from();
    }
</script>
</body>
</html>
